package com.baizhi.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.baizhi.entity.Address;
import com.baizhi.entity.User;
import com.baizhi.service.AddressService;
import com.baizhi.service.impl.AddressServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 地址相关的action操作
 * @author 王雪洋
 * @Time 2017年5月4日 下午4:32:51
 */
public class AddressAction extends ActionSupport{
	private Address address = new Address();
	private List<Address> alist;
	private Integer aid;
	private AddressService as = new AddressServiceImpl();
	private HttpSession session = ServletActionContext.getRequest().getSession();
	private Integer addressid;
	/*
	 * 根据用户ID查询地址
	 */
	public String getAddressByUserId(){
		User user = (User)session.getAttribute("user");
		if(user != null){
			alist = as.findAddressByUserId(user.getUserid());
		}
		return SUCCESS;
	}
	/*
	 * 根据地址ID查找地址  王雪洋
	 */
	public String findAddressById(){
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user != null){
			alist = as.findAddressByUserId(user.getUserid());
		}
		address = as.findAddressById(aid);
		return SUCCESS;
	}
	
	/*
	 *新建地址 
	 */
	public String addAdderss(){
		int i = 0;
		if(aid == null){
			User user = (User)session.getAttribute("user");
			address.setUseroid(user.getUserid());
			i = as.addAddress(address);
			if(i!=0){
				//记录地址id
				/*session.setAttribute("addressid", i);*/
				addressid = i;
			}
		}else{
			/*session.setAttribute("addressid", aid);*/
			//记录地址ID
			addressid = aid;
		}
		return SUCCESS;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
	public Integer getAid() {
		return aid;
	}
	public void setAid(Integer aid) {
		this.aid = aid;
	}
	public List<Address> getAlist() {
		return alist;
	}
	public void setAlist(List<Address> alist) {
		this.alist = alist;
	}
	public Integer getAddressid() {
		return addressid;
	}
	public void setAddressid(Integer addressid) {
		this.addressid = addressid;
	}
}
